package com.example.sqlist03;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    AlertDialog alertDialog;
    private List<Student>list= new ArrayList<>();
    private TextView textView;
    private RecyclerView recycle;
    private Button button;
    mySQList mySQList;
    SQLiteDatabase database;//数据库，是时候调用数据了
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        sqlist();
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        recycle.setLayoutManager(linearLayoutManager);
        main_Adapter main_adapter=new main_Adapter(list);
        recycle.setAdapter(main_adapter);
        main_an_niu();
    }
    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        recycle = (RecyclerView) findViewById(R.id.recycle);
        button = (Button) findViewById(R.id.button);
    }
    private void sqlist() {
        mySQList=new mySQList(this,"student.db",null,1);//上下文,文件名，厂房，版本号
        database=mySQList.getWritableDatabase();//读写权限任你调
        Cursor cursor=database.query("student",null,null,null,null ,
                null ,null,null);
        if (cursor.moveToFirst()){
            do {
                // 返回列名为columnName对应的列索引（索引从0开始），如果不存在返回-1
                int id=cursor.getInt(cursor.getColumnIndex("_id"));//简称索引
                String name=cursor.getString(cursor.getColumnIndex("name"));
                int age=cursor.getInt(cursor.getColumnIndex("age"));
                list.add(new Student(name,id,age));
            }while (cursor.moveToNext());
            }
        cursor.close();
        database.close();
        }

    public void tianjia(View view) {
        View view_2= LayoutInflater.from(MainActivity.this).inflate(R.layout.main_student,null);
        textView = (TextView) findViewById(R.id.textView);
        recycle = (RecyclerView) findViewById(R.id.recycle);
        button = (Button) findViewById(R.id.button);
       Button button2 = findViewById(R.id.clos);
        new AlertDialog.Builder(this).setTitle("添加数据").setView(view_2)
                .setPositiveButton("添加",
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String name=((EditText)view_2.findViewById(R.id.edname)).getText().toString();
                int age=Integer.parseInt(((EditText)view_2.findViewById(R.id.edage)).getText().toString());
                //ContentValues类和Bundle类很类似
                ContentValues values=new ContentValues();
                values.put("name",name);
                values.put("age",age);
                database=mySQList.getWritableDatabase();
                database.insert("student",null,values);
                list.clear();
                Cursor cursor=database.query("student",null,null,null,null,
                        null,null);
                if (cursor.moveToFirst()){
                    do {
                        // 返回列名为columnName对应的列索引（索引从0开始），如果不存在返回-1
                        int id=cursor.getInt(cursor.getColumnIndex("_id"));//简称索引
                        name=cursor.getString(cursor.getColumnIndex("name"));
                        age=cursor.getInt(cursor.getColumnIndex("age"));
                        list.add(new Student(name,id,age));
                    }while (cursor.moveToNext());
                }
                recycle.getAdapter().notifyDataSetChanged();
                database.close();
                cursor.close();
            }
        }).setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create().show();
    }
    private void main_an_niu(){
        ((main_Adapter)recycle.getAdapter()).setClickInterface(new main_Adapter.OnClickInterface() {
            @Override
            public void onclick(View view, int position) {

                switch (view.getId()){
                    case R.id.clos:
                        database=mySQList.getWritableDatabase();
                        database.delete("student","_id=?",new String[]{String.valueOf(list.get(position).get_id())});
                        list.clear();
                        Cursor cursor=database.query("student",null,null,null,null,
                                null,null);
                        if (cursor.moveToFirst()){
                            do {
                                // 返回列名为columnName对应的列索引（索引从0开始），如果不存在返回-1
                                int id=cursor.getInt(cursor.getColumnIndex("_id"));//简称索引
                               String name=cursor.getString(cursor.getColumnIndex("name"));
                               int age=cursor.getInt(cursor.getColumnIndex("age"));
                                list.add(new Student(name,id,age));
                            }while (cursor.moveToNext());
                        }
                        //notifyDataSetChanged()可以在修改适配器绑定的数组后，不用重新刷新Activity
                        recycle.getAdapter().notifyDataSetChanged();
                        database.close();
                        cursor.close();
                        break;
                    case R.id.gengxin:
                        View view_2= LayoutInflater.from(MainActivity.this).inflate(R.layout.main_student,null);
                        view_2.findViewById(R.id.edname);
                        view_2.findViewById(R.id.edage);
                        new AlertDialog.Builder(MainActivity.this).setTitle("更新数据").setView(view_2)
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).setPositiveButton("更新", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String name=((EditText)view_2.findViewById(R.id.edname)).getText().toString();
                                int age=Integer.parseInt(((EditText)view_2.findViewById(R.id.edage)).getText().toString());
                                //ContentValues类和Bundle类很类似
                                ContentValues values=new ContentValues();
                                values.put("name",name);
                                values.put("age",age);
                                database=mySQList.getWritableDatabase();
                                database.update("student",values,"_id=?",
                                        new String[]{String.valueOf(list.get(position).get_id())});
                                list.clear();
                                Cursor cursor=database.query("student",null,null,null,null,
                                        null,null);
                                if (cursor.moveToFirst()){
                                    do {
                                        // 返回列名为columnName对应的列索引（索引从0开始），如果不存在返回-1
                                        int id=cursor.getInt(cursor.getColumnIndex("_id"));//简称索引
                                        name=cursor.getString(cursor.getColumnIndex("name"));
                                        age=cursor.getInt(cursor.getColumnIndex("age"));
                                        list.add(new Student(name,id,age));
                                    }while (cursor.moveToNext());
                                }
                                recycle.getAdapter().notifyDataSetChanged();
                                database.close();
                                cursor.close();
                            }
                        }).create().show();
                        break;
                }
            }
        });
    }
}